package com.hb.game.pk.ui.activity.game.szww;

import android.content.Intent;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hb.game.pk.APP;
import com.hb.game.pk.R;
import com.hb.game.pk.adpter.OpenRedListAdpter;
import com.hb.game.pk.api.Host;
import com.hb.game.pk.api.RedBag;
import com.hb.game.pk.base.BaseActivity;
import com.hb.game.pk.beans.ClickRedDean;
import com.hb.game.pk.network.RetrofitCreateHelper;
import com.hb.game.pk.ui.activity.game.red.TotalRedActivity;
import com.hb.game.pk.utils.SpUtils;
import com.hb.game.pk.utils.StringUtils;
import com.hb.game.pk.utils.ToastUtils;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class OpenRedActivity extends BaseActivity<ClickRedDean> {
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.iv_boom)
    ImageView ivBoom;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_rule)
    TextView tvRule;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_count)
    TextView tvCount;
    @BindView(R.id.tv_more)
    TextView tvMore;
    List<ClickRedDean.DataBean.ListBean> list;
    String clickHbId;
    private SoundPool soundPool;
    private HashMap<Integer, Integer> soundPoolMap;
    private CountDownTimer timer;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_open_red;
    }

    @Override
    protected int getRootViewId() {
        return R.id.layout_root;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return R.id.rv_list;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        Intent intent = getIntent();
        clickHbId = intent.getStringExtra("clickHbId");
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        soundPool = new SoundPool(1, AudioManager.STREAM_ALARM, 0);
        if (SpUtils.getBoolean(this, "sound", true)) {
            soundPoolMap = new HashMap<Integer, Integer>();
            soundPoolMap.put(1, soundPool.load(OpenRedActivity.this, R.raw.coin, 1));
            soundPoolMap.put(2, soundPool.load(OpenRedActivity.this, R.raw.bao, 1));
            soundPoolMap.put(3, soundPool.load(OpenRedActivity.this, R.raw.su, 1));
        }
    }

    @Override
    protected void onRefreshData() {
        super.onRefreshData();
        call = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).slClickResult(clickHbId);
        call.enqueue(new Callback<ClickRedDean>() {
            @Override
            public void onResponse(Call<ClickRedDean> call, Response<ClickRedDean> response) {
                ClickRedDean body = response.body();
                if (body == null) {
                    ToastUtils.showToast("请求错误");
                    return;
                }
                if (body.getStatus() != 1) {
                    ToastUtils.showToast(body.getInfo());
                    return;
                }
                ClickRedDean.DataBean data = body.getData();

                if (data.getIs_selfin() == 1) {
                    if (data.getCheck() == 1) {
                        // 不管
                    } else {

                        if (data.getIs_bom() == 1) {
                            ivBoom.setVisibility(View.VISIBLE);
                            timer = new CountDownTimer(3 * 1000, 1000) {
                                @Override
                                public void onTick(long l) {
                                    if (l / 1000 == 0) {
                                        ivBoom.setVisibility(View.GONE);
                                    }
                                }

                                @Override
                                public void onFinish() {

                                }
                            };
                            timer.start();
                        }

                        if (SpUtils.getBoolean(OpenRedActivity.this, "sound", true)) {
                            if (data.getIs_bom() == 1) {
                                // 中雷
                                soundPool.play(soundPoolMap.get(2), 1, 1, 0, 0, 1);
                                ivBoom.setVisibility(View.VISIBLE);

                            } else {
                                // 钱的声音
                                soundPool.play(soundPoolMap.get(1), 1, 1, 0, 0, 1);
                            }
                        }
                    }
                } else {
                    // 不管
                }

                list = data.getList();
                String trim = StringUtils.getString(data.getAvatar());
                int res = 0;
                if (StringUtils.isEmpty(trim)) {
                    res = R.mipmap.ic_head_normal;
                } else {
                    int i = Integer.parseInt(trim);
                    res = APP.headResource.get(i - 1);
                }
                ivHead.setBackgroundResource(res);
                tvName.setText(data.getUsername());
                tvRule.setText(String.format("%.2f", (Float.parseFloat(data.getMoney()) / 100)) + " - " + data.getBom_num());
                tvMoney.setText(String.format("%.2f", data.getSelfmoney() / 100f) + "");
                tvCount.setText("已领取" + data.getRecive_num() + "/" + data.getNums());
                initAdapter();
            }

            @Override
            public void onFailure(Call<ClickRedDean> call, Throwable t) {
                ToastUtils.showToast("网络请求错误");
            }
        });
    }

    @Override
    protected void initAdapter() {
        if (mAdapter == null) {
            mAdapter = new OpenRedListAdpter(R.layout.item_open_red_list);
            mAdapter.setNewData(list);
            TextView view = new TextView(this);
            view.setText("查看我的红包记录");
            view.setTextColor(getResources().getColor(R.color.c_516FA2));
            view.setGravity(Gravity.CENTER_HORIZONTAL);
            view.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.px30));
            mAdapter.setFooterView(view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(TotalRedActivity.class);
                }
            });
        } else {
            mAdapter.setNewData(list);
        }
        super.initAdapter();
    }

    @Override
    public boolean isNeedLoadMore() {
        return false;
    }

    @OnClick({R.id.ll_back, R.id.tv_more})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                this.finish();
                break;
            case R.id.tv_more:

                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }
    }
}
